__author__ = 'Administrator'

import pandas as pd
import os

prod = '12'
b_rate = 0.00025
s_rate = 0.00125

wfname = "D:\\data\\statistic\\t0\\t0_prod"+prod+".Detail.csv"
rpath = "P:\\NetValue\\production"+prod+"\\portfolio\\actTrade\\Intraday\\"

def GetTradeDetail():
	cnt=0
	for fname in os.listdir(rpath):
		if "actTrade.intraday.csv" in fname:
			print(fname)
			cname = rpath + fname
			df = pd.read_csv(cname)
			fdate = fname[0:4]+"/"+fname[4:6] + "/" + fname[6:8]
			df['date'] = fdate
			df['hcash'] = -df['actShr'] * df['expx']
			df = df[['date','#tk','actShr','expx','hcash']]
			df['comm'] = df['hcash'].apply(lambda x: -x*b_rate if x <0 else x*s_rate)
			df['real_hcash'] = -df['comm'] + df['hcash']
			if cnt == 0:
				df.to_csv(wfname,index=False,header=True,mode='w')
				cnt+=1
			else:
				df.to_csv(wfname,index=False,header=False,mode='a')

def GetTradePnl():
	df = pd.read_csv(wfname)
	df = df.groupby("date")['real_hcash'].sum().to_frame()
	df.columns = ['pnl']
	df.index = pd.to_datetime(df.index.astype(str))
	#df = df.resample("M").sum()
	df = df.groupby(df.index.month).sum()
	df.to_csv(wfname.replace("Detail","pnl_by_month"))

def GetTradePnlByDate():
	df = pd.read_csv(wfname)
	df = df.groupby("date")['real_hcash'].sum().to_frame()
	df.columns = ['pnl']
	df.index = pd.to_datetime(df.index.astype(str))
	#df = df.resample("M").sum()
	#df = df.groupby(df.index.day).sum()
	df.to_csv(wfname.replace("Detail","pnl_by_day"))

if __name__ == "__main__":
	#GetTradeDetail()
	#GetTradePnl()
	GetTradePnlByDate()
